#include<bits/stdc++.h>
using namespace std;
const int maxn = 300'000+7;
int a[maxn], n, m;
bool check(int k) {
  int prev = 0;
  for(int i=0; i<n; ++i) {
    int li=a[i], ri=a[i]+k;
    if(li<=prev&&prev<=ri || li<=prev+m&&prev+m<=ri) continue;
    if(li<prev) return false;
    else prev = li;
  }
  return true;
}
int main() {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  cin>>n>>m; for(int i=0; i<n; ++i) cin>>a[i];
  int l=-1, r=m, mid;
  while(l+1<r) {
    mid = (l+r)>>1;
    if(check(mid)) r = mid;
    else l = mid;
  }
  cout<<r<<'\n';
}
